//
//  GFYjDetailViewController.m
//  Qyer
//
//  Created by dlios on 15/10/23.
//  Copyright (c) 2015年 GF. All rights reserved.
//

#import "GFYjDetailViewController.h"

@interface GFYjDetailViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong)GFHeader *headerView;
@property (nonatomic, strong)NSString *dateString;
@end

@implementation GFYjDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _headerView = [[GFHeader alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 190 * HEIGHT)];
    [self.view addSubview:_headerView];
    
    _tableArray = [NSMutableArray array];
    _sectionArray = [NSMutableArray array];
    _table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    [_table setDelegate:self];
    [_table setDataSource:self];
    [_table setTableHeaderView:_headerView];
    [self.view addSubview:_table];
    [self getAfn];
}

- (void)getAfn{
    [AFN getdataWithString:_afnString block:^(id block) {
        NSString *string = [NSString stringWithFormat:@"%@ / %@", [block objectForKey:@"start_date"], [block objectForKey:@"end_date"]];
        [_headerView gettitleString:[block objectForKey:@"name"] time:string bigImage:_bigIamge userImage:_userDic];
        for (NSDictionary *dic in [block objectForKey:@"trip_days"]) {
            GFYjModel *model = [GFYjModel modelWithDictionary:dic];
            [_sectionArray addObject:model];
            for (NSDictionary *tableDictionary in [dic objectForKey:@"nodes"]) {

#pragma mark-如果这是一个tableView嵌套tableView那么内侧tableView的数据将在这里进行解析
                
                for (NSDictionary *tableDic in [tableDictionary objectForKey:@"notes"]) {
                    GFYjModel *model = [GFYjModel modelWithDictionary:tableDic];
                    model.height = [StringWithHeight HeightWithText:model.des width:SCREEN_WIDTH - 10 * WIDTH font:[UIFont systemFontOfSize:WIDTH * 15.]];
                    [_tableArray addObject:model];
                }
            }
        }
        [_table reloadData];
    } BLOCK:^(id BLOCK) {
        
    }];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    GFYjModel *model = [_sectionArray objectAtIndex:section];
    NSString *preString = @"DAY";
    NSString *comString = [NSString stringWithFormat:@"%@%@",preString, model.days];
    return comString;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _sectionArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _tableArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    GFYjModel *model = [_tableArray objectAtIndex:indexPath.row];
    if (!model.photo) {
        if (model.des != NULL) {
            return model.height + 10 * HEIGHT;
        }else{
            return 0;
        }
    }else{
        if (model.des != NULL) {
            return 190 * HEIGHT + model.height;
        }else{
            return 190 * HEIGHT;
        }
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *guoFeng = @"cell";
    GFYjDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:guoFeng];
    if (!cell) {
        cell = [[GFYjDetailTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:guoFeng];
    }
    GFYjModel *model = [_tableArray objectAtIndex:indexPath.row];
    [cell getYjModel:model];
    return cell;
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
